package com.study.servlet.admin;

import com.study.dao.AwardInfoDAO;
import com.study.dao.impl.AwardInfoDAOImpl;
import com.study.pojo.AwardInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @PackgeName: com.study.servlet.admin
 * @ClassName: CountryAddServlet
 * @Author: 严志祥
 * Date: 2022-05-06 14:43
 * project name: web_project_medal
 * @Version:
 * @Description:
 */
@WebServlet("/admin/award-update.let")
public class AwardUpdateServlet extends HttpServlet {
    private AwardInfoDAO dao = new AwardInfoDAOImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.reset();
        resp.setContentType("application/json;charset=utf-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            AwardInfo a = new AwardInfo();
            System.out.println("a");
            String id =req.getParameter("awardId");
            System.out.println(id);
            int sId = Integer.parseInt(req.getParameter("sportingId"));
            System.out.println("c");
            int ranking = Integer.parseInt(req.getParameter("awardRanking"));
            System.out.println("d");
            String dateStr = req.getParameter("awardDate");
            System.out.println("e");
            a.setAwardId(Integer.parseInt(id));
            System.out.println("f");
            a.setSportingId(sId);
            System.out.println("g");
            a.setAthletesId(Integer.parseInt(req.getParameter("athletesId")));
            System.out.println("h");
            a.setAwardRanking(ranking);
            System.out.println("i");
            a.setAwardGrade(req.getParameter("awardGrade"));
            System.out.println("j");
           a.setAwardDate(sdf.parse(dateStr));
            System.out.println("k");
            try {
                System.out.println(444);
                dao.updateById(a);
                resp.getWriter().print("{\"ok\":true}");
            } catch (Exception ex) {
                throw new ServletException("编号重复", ex);
            }
        }catch (Exception e){
            resp.getWriter().print("{\"ok\":false,\"msg\":\""+e.getMessage()+"\"}");
        }
        resp.flushBuffer();
    }
}
